/**
Company: Sarkar software technologys
WebSite: http://www.sarkar.cn
Author: yeganaaa
Date : 5/16/18
Time: 7:05 PM
 */

package Cn.Sarkar.IMS.Entities.DataSources

import Cn.Sarkar.Configuration.makeConfig
import Cn.Sarkar.DataVersionManager
import Cn.Sarkar.Entities.Common.ConnectionPool
import Cn.Sarkar.EntityDream.CoreEngine.RDBMS.Core.IDBTable
import Cn.Sarkar.EntityDream.CoreEngine.RDBMS.MySql.MySqlDataContext
import Cn.Sarkar.EntityDream.Pipeline.Extension.installFeature
import Cn.Sarkar.IMS.DataBaseDefinations.*
import Cn.Sarkar.logger
import com.mysql.cj.MysqlConnection
import java.io.File
import java.sql.DriverManager

class MySqlDataSource : MySqlDataContext(pool.getConnection()) {
    override val tables: Array<IDBTable> = arrayOf(
            Areas,
            BuyOrderItems,
            BuyOrders,
            BuyTradeItems,
            BuyTrades,
            Descriptions,
            DomainProducts,
            IMSApplicationDomains,
            Names,
            Organs,
            PriceItems,
            PriceTables,
            ProductCompositionItems,
            ProductCompositions,
            Products,
            SellOrderItems,
            SellOrders,
            SellTradeItems,
            SellTrades,
            ShiftHistories,
            Stamps,
            StorageRoomItems,
            StorageRooms,
            Types,
            Units
    )

    init {
        pipeLine.installFeature(logger())
        pipeLine.installFeature(DataVersionManager())
    }

    companion object {
        private val pool by lazy {
            val config = makeConfig("Configuration${File.separator}IMSDataConfigurations.json", ConnectionPool.Configuration::class) { ConnectionPool.Configuration(
                    database = "mysql",
                    driverClass = "com.mysql.jdbc.Driver",
                    userName = "root",
                    password = "12345678",
                    jdbcUrl = "jdbc:mysql://127.0.0.1:3308/SarkarIMS"
            ) }
            ConnectionPool(config)
        }
    }
}